// pages/mine/mine.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  qrcode = require('../../utils/qrcode.js'),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    follows:0,
    fans:0,
    qrsrc:'',
    funcation1: [
      {
        src: '../pic/order.png',
        title: '员工管理',
      },


      // {
      //   src: '../pic/waitpay.png',
      //   title: '待付款',
      // },
      // {
      //   src: '../pic/waitgoods.png',
      //   title: '待发货',
      // },
      // {
      //   src: '../pic/waitget.png',
      //   title: '待收货',
      // },
      // {
      //   src: '../pic/waitpost.png',
      //   title: '待还书',
      // },
      // {
      //   src: '../pic/waitfinish.png',
      //   title: '待退押金',
      // },
      // {
      //   src: '../pic/finish.png',
      //   title: '已完成',
      // },
      // {
      //   src: '../pic/refund.png',
      //   title: '借书押金',
      // },
    ],
    funcation2: [
      {
        src: '../pic/order.png',
        title: '全部订单',
      },
      {
        src: '../pic/waitpay.png',
        title: '待付款',
      },
      {
        src: '../pic/waitgoods.png',
        title: '待发货',
      },
      {
        src: '../pic/waitget.png',
        title: '待收货',
      },
      {
        src: '../pic/received.png',
        title: '已收货',
      },
      {
        src: '../pic/waitfinish.png',
        title: '待退款',
      },
      {
        src: '../pic/finish.png',
        title: '已完成',
      },
      {
        src: '../pic/cancelorder.png',
        title: '已取消',
      },
    ],
    showshare:!1,
    islogin: !1,
    avatarUrl: "../pic/avatar.png",//用户头像
    nickName: "",//用户昵称
  },
  //
  getsetting(){
    wx.getSetting({
      success: (res) => {
        console.log('sesese',res.authSetting)
      },
    })
  },
  tofollowxiaoba(){
    wx.navigateTo({
      url: '../xiaoba/xiaoba',
    })
  },

  getPhoneNumber(e){
    let {hasphone,sendshow}=that.data
    api.getphone({edata:e.detail.encryptedData,iv:e.detail.iv},res=>{
      if(res.data.issuccess==1){
        let data = JSON.parse(res.data.data);
        let isgetphone=true
        wx.setStorageSync('isgetphone', isgetphone)
        that.setData({hasphone:!0,sendshow:!1,isgetphone})
        console.log(data.phoneNumber.phoneNumber,'data')
        let phone=data.phoneNumber
        console.log(phone,'phonephonephone');
        //更新用户手机号
        api.getuserphone(phone,res=>{
          if(res.data.issuccess==1){
            console.log(res.data,'phone')
          }
        })
      }
    })
  },

  getUserProfile(res) {
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo, 'resres');
        let userinfo = res.userInfo
        if (res.userInfo) {
          wx.setStorageSync("userinfo", userinfo);
          api.getnameurl({ avatarUrl: userinfo.avatarUrl, nickName: userinfo.nickName }, res => {
            if (res.data.issuccess == 1) {
              that.setData({ islogin: !0,avatarUrl: userinfo.avatarUrl,nickName:userinfo.nickName }, () => {
                app.logininfo.user.avatarUrl = userinfo.avatarUrl
                app.logininfo.user.nickName = userinfo.nickName
              })
            } else {
              wx.showToast({
                title: '授权失败，请重新授权',
                icon: 'none'
              })
            }
            // page.setData({hideadd:!1});
          });
        } else {
        }
      },
      fail(res) {
      },
    })
  },
  // getuserinfo(e){
  //   // console.log(e,'eeeeeeeeeee')
  //   let userinfo=e.detail.userInfo
  //   that.setData({userinfo})
 
  // },
  //
  toidentity(){
    wx.navigateTo({
       url: '../identity/identity',
    })
  },
  sends(){
    api.ft_sendts1({},res=>{
      console.log('res',res.data)
    })
  },
  //
  authMsg(event){
    wx.requestSubscribeMessage({
    tmplIds: [
      // 'at8Q9OkKK3RUQDaTipv1llfgnxmi49CG-vH2Sk6pqpw',
              'wJ6U3DozZdVEET7P2x4cmYXr_v9JSqFUlhcK0GFsLq4'],
    success: function (res) {
        console.log(res)
    },
    fail: function (err) {
        console.log(err)
    }
    })
  },
  //跳转排行榜
  godays(){
    wx.navigateTo({
      url:"../days/days",
    })
  },
  goshoporder(){
    wx.navigateTo({
      url: '../shopOrder/shopOrder',
    })
  },
  //跳转关注
  gofollow(){
    console.log(app,'appapp')
    let id=app.logininfo.userid
    wx.navigateTo({ 
      url: '../follows/follows?id='+id,
    })
  },
  //跳转粉丝
  gofan(){
    let id=app.logininfo.userid
    wx.navigateTo({
      url: '../fans/fans?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    let logininfo = app.logininfo;
    let user = app.logininfo.user,appletname=app.logininfo.appletname;
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    let {shopid,shopname,nickname,permission,avatar}=user
    console.log(user,'userinfo');
    //wx.getStorageSync("userinfo")
    if (user.avatar) {
      that.setData({ permission,islogin: !0,ID:user.id, shopid,shopname, avatarUrl:avatar,nickName:nickname,})
    }
    that.setData({ userid: logininfo.userid ,usertype: user.usertype,user,appletname})
    // this.getBOOKSdots();
    // this.getGOODSdots();
  },
  formsubmit(e) {
    api.gatherformid(e,res => {
    
    }) 
  },
  getBOOKSdots() {
    let type='BOOKS'
    api.mp_getminestate(type,res=>{
      if(res.data.issuccess==1){
        let dots = [res.data.data.length,0,0,0,0];
        res.data.data.forEach(x=>{
          if (x == 'NO_PAY') {
              dots[1]++;
            } else if (x == 'PAY_SUCCESS') {
              dots[2]++;
            } else if (x == 'SHIPPED') {
              dots[3]++
            } else if(x == 'RECEIVED') {
              dots[4]++
            }
        })
        that.setData({ dots })
      }
    })
  },
  getGOODSdots() {
    let type='GOODS'
    api.mp_getminestate(type,res=>{
      if(res.data.issuccess==1){
        let goodsdots = [res.data.data.length,0,0,0,0];
        res.data.data.forEach(x=>{
          if (x == 'NO_PAY') {
            goodsdots[1]++;
            } else if (x == 'PAY_SUCCESS') {
              goodsdots[2]++;
            } else if (x == 'SHIPPED') {
              goodsdots[3]++
            } else if(x == 'RECEIVED') {
              goodsdots[4]++
            }
        })
        that.setData({ goodsdots })
      }
    })
  },
    // // 邀请
    // invitation(){

    //   that.setData({
    //     showmodal:1
    //   })
    //   wx.showLoading({
    //     title: '加载中',
    //   })
    //   wx.getSystemInfo({
    //     success: (res) => {
    //       new qrcode({
    //         width: res.screenWidth * 0.65,
    //         height: res.screenWidth * 0.65,
    //         canvasId: 'canvas',
    //         text: 'https://jiebandushu.com/share?id=' + that.data.id,
    //         callback: res => {
    //           wx.hideLoading();
  
    //         }
    //       })
    //     },
    //   })
    // },


      // 邀请
  invitation(){
  //   let {qrsrc,userid} = this.data;
  //   console.log(userid,'useriduserid');
    
  //   if(qrsrc.length>0){
  //     that.setData({ showmodal:1, })
  //     return;
  // }
  //   that.setData({
  //     showmodal:1,loadingimg:!0
  //   })
  //   wx.showLoading({
  //     title: '图片生成中...',
  //   })
  //   wx.getSystemInfo({
  //     success: (res) => {     
  //       console.log(res,'resres');
  //       let swidth=res.screenWidth,sheight=res.screenHeight
  //       new qrcode({
  //         width: swidth * 0.316,
  //         height: swidth * 0.316,
          
  //         canvasId: 'canvas',
          
  //         text: 'https://www.jihemu.com/bookapi/share?id='+userid,
  //         // text: 'https://www.jihemu.com/bookapi/share?id=2',
  //         // https://www.jihemu.com/bookapi/share?id=2
  //         callback: res => {
  //           wx.canvasToTempFilePath({
  //             canvasId: 'canvas',
  //             quality: 0,
  //             success:res1=>{
  //               let tempFilePath =res1.tempFilePath
  //                 //  let canvas.drawImage(tempFilePath)s
  //                 that.setData({qrsrc:tempFilePath,loadingimg:!1},()=>{
  //                   wx.hideLoading();
  //                 })
  //             }
  //           }, )

  //         }
  //       })
  //     },
  //   })

    let {userid} =that.data;
      that.setData({ showshare: !0 })
      wx.showLoading({
        title: '加载中',
      })
      wx.getSystemInfo({
        success: (res) => {
          new qrcode({
            width: res.screenWidth * 0.65,
            height: res.screenWidth * 0.65,
            canvasId: 'canvas',
            text: 'https://www.jihemu.com/bookapi/share?id='+userid,
            callback: res => {
              wx.hideLoading();
            
            }
          })
        },
      })



  },


    closemodal(){
      // that.setData({
      //   showmodal: 0
      // })
       that.setData({showshare:!1})
    },
  //保存图片
    savepic(){
          wx.canvasToTempFilePath({
            canvasId: 'canvas',
            quality: 0,
            success: res => {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: res1 => {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none'
                  })
                },
              })
    
    
            }
          }, that)
        },
  toaddress() {
    wx.navigateTo({
      url: '../address/address?a=1',
    })},
  tohelps() {
    wx.navigateTo({
      url: '../helps/helps',
    })
  },
  todeposit() {
    wx.navigateTo({
      url: '../deposit/deposit',
    })
  },
  tocollection() {
    wx.navigateTo({
      url: '../collection/collection',
    })
  },
  toorder(e){
    let {index} = e.currentTarget.dataset;
    if(index==7){
      this.todeposit()
      return;
    }
    console.log(index,e,"index");
    wx.navigateTo({
      url: '../order/order?o='+index,
    })
  },
  toshopOrder(e){
    let {index} = e.currentTarget.dataset;
    console.log(index,e,"index");
    wx.navigateTo({
      url: '../shopOrder/shopOrder?o='+index,
    })
  },
  to(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../${url}/${url}`,
    })
  },
  callServer() {
    // let phone=app.logininfo.phone
    // console.log('13960823087')
    wx.makePhoneCall({
      phoneNumber: '13960823087',
    })
  },
  toHehuoren:function(){
    wx.navigateTo({
      url: './hehuoren/hehuoren',
    })
  },
  gobooks(){
    wx.switchTab({
      url: '../books/books',
    })
  },
  tosampleReels(){
    wx.navigateTo({
      url: '../sampleReels/sampleReels?type=1',

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // let token = wx.getStorageSync('token')
    console.log( wx.getStorageSync('token'),'tokentoken')
    api.mine_getfollowandfans(res=>{
      if(res.data.issuccess==1){
        let{follows,fans} = res.data.data;
        that.setData({ follows, fans})
      }
    })
    that.getGOODSdots() 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,
    }
  }
})